package fr.efrei.international.java.assignment.two;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class DataLoader {

	ArrayList<CharacterData> datalist = new ArrayList<CharacterData>();
	ArrayList<UserData> usrList = new ArrayList<UserData>();
	UserData ud = new UserData(1);

	public DataLoader() {

		String str = null;
		String[] s = null;
		FileInputStream in = null;
		try {
			in = new FileInputStream(".\\chinese.txt");
			InputStreamReader fr = new InputStreamReader(in, "Unicode");
			BufferedReader bi = new BufferedReader(fr);
			while ((str = bi.readLine()) != null) {
				CharacterData d = new CharacterData();
				s = str.split("	");
				d.setLevel(Integer.valueOf(s[0]));
				d.setId(Integer.valueOf(s[1]));
				d.setHanzi(s[2]);
				d.setPinyin(s[3]);
				d.setTranslations(s[4]);
				datalist.add(d);
			}
		} catch (Exception e) {
			System.out.println("cannt find the file!!");
			System.exit(-1);
		}

		try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public CharacterData randomChose(int level) {
		int b = 0;
		switch (level) {
		case 1:
			while (((b = (int) (Math.random() * 150))) == 150)
				;

			break;
		case 2:
			while (((b = (int) (Math.random() * 300)) == 300)
					|| ((b = (int) (Math.random() * 300)) < 150))
				;
			break;

		case 3:
			while (((b = (int) (Math.random() * 599)) == 599)
					|| ((b = (int) (Math.random() * 599)) < 300))
				;
			break;
		case 4:
			while (((b = (int) (Math.random() * 1200)) == 1200)
					|| ((b = (int) (Math.random() * 1200)) < 599))
				;
			break;
		case 5:
			while (((b = (int) (Math.random() * 2500)) == 2500)
					|| ((b = (int) (Math.random() * 2500)) < 1200))
				;
			break;
		case 6:
			while (((b = (int) (Math.random() * 5000)) == 5000)
					|| ((b = (int) (Math.random() * 5000)) < 2500))
				;
			break;
		default:
			break;
		}

		return datalist.get(b);
	}
}
